//
//  CAPOauthModel.swift
//  CAPSwift
//
//  Created by captain on 16/4/20.
//  Copyright © 2016年 captain. All rights reserved.
//

import UIKit

class CAPOauthModel: NSObject,NSCoding {
//    "access_token" = "2.00U83EMGzDGiCCe512cf7b30Tiju_D";
//    "expires_in" = 157679999;
//    "remind_in" = 157679999;
//    uid = 5675152608;
    var access_token: String?
    var expires_in: Int = 0{
        //返回给expires_in赋值的时候会走didSet方法
        didSet{
            expires_date = NSDate().dateByAddingTimeInterval(Double(expires_in))
        }
    }
    
    var uid: String?
    //自己定义一个，过期时间。便于以后比较是否过期
    var expires_date: NSDate?
    
    
    //用户头像
    var avatar_large: String?
    
    //用户昵称
    var screen_name: String?
    
    
    init(dict: [String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    //空实现一个un
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
    }
    
    // MARK: - 重写description方法，可以展示model的内容。不然是地址
    override var description: String {
        let keys = ["access_token","expires_date","uid","avatar_large","screen_name"]
        
        return dictionaryWithValuesForKeys(keys).description
        
    }
    
    // MARK: - 归档
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(expires_date, forKey: "expires_date")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
    }
    
    // MARK: - 接档
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_date = aDecoder.decodeObjectForKey("expires_date") as? NSDate
        uid = aDecoder.decodeObjectForKey("uid") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
    }
    
    
    
    
}
